@using Abp.Application.Services.Dto;
@using FwProject.Core.Restaurant
@using X.PagedList
@using FwProject.Distribution.Dto;
@model IPagedList<GenerateReceiptOutputDto>
@{
    ViewBag.Title = "制作收据";
}
@section styles{
    <link href="~/Theme/css/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="~/Theme/css/plugins/datapicker/datepicker3.css" rel="stylesheet">
    <style type="text/css">
        .input-group[class*=col-] {
            padding: 0 15px;
        }

        .fl {
            float: left;
        }

        @@media(max-width:1280) {
        }
    </style>
}
<div class="row wrapper wrapper-content animated fadeInRight">
    <div class="col-sm-12">
        <div class="ibox float-e-margins">
            <div class="ibox-title">
                <h5>@ViewBag.Title</h5>
                <div class="ibox-tools">
                    @if (IsGranted(FwProject.Authorization.PermissionNames.Finance_Operation_MakingReceipt))
                    {
                        <a class="btn btn-primary btn btn-sm" id="btn">生成收据</a>
                    }
                    else
                    {
                        <a class="btn btn-default btn btn-sm">暂无生成收据权限</a>
                    }

                </div>
            </div>
            <div class="ibox-content">
                <div class="row m-b-sm m-t-sm">
                    <form id="form0" class="form-horizontal" asp-controller="Receivable" asp-action="MakingReceipt" data-ajax="true" data-ajax-update="#data_list" data-ajax-method="Get" data-ajax-mode="replace" data-ajax-success="LoadDataSuccessMessage" data-ajax-begin="AjaxBegin">
                        <input type="hidden" id="size" name="size" value="0" />
                        <div class="form-group">
                            <div class="col-md-6 col-lg-4">
                                <label class="control-label col-md-4 col-lg-3">配送日期：</label>
                                <div class="input-group fl">
                                    <select id="data_select" class="input-s form-control"></select>
                                </div>
                                <div class="input-daterange input-group fl" id="datepicker" style="width:226px;margin-left:10px;">
                                    <input type="text" name="beginTime" id="beginTime" class="input-md form-control" style="width:100px" autocomplete="off">
                                    <span class="input-group-addon">到</span>
                                    <input type="text" name="endTime" id="endTime" class="input-md form-control" style="width:100px" autocomplete="off" />
                                </div>
                            </div>
                            <div class="col-md-6 col-lg-4">
                                <div class="input-group col-md-offset-2">
                                    <button type="submit" class="btn btn-w-m btn-success">搜索</button>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-md-6 col-lg-4">
                                <label class="control-label col-md-4 col-lg-3">餐厅：</label>
                                <div class="input-group">
                                    <input type="text" name="keyword" class="input-sm form-control" value="" autocomplete="off">
                                </div>
                            </div>
                            <div class="col-md-6 col-lg-4">
                                <label class="control-label col-md-4 col-lg-3">线路：</label>
                                <div class="input-group">
                                    @Html.DropDownList("Line2", ViewBag.Line as IEnumerable<SelectListItem>, "全部", new { @class = "form-control" })
                                </div>
                            </div>
                            <div class="col-md-6 col-lg-4">
                                <label class="control-label col-md-4 col-lg-3">司机：</label>
                                <div class="input-group">
                                    <input type="text" id="dropdown-driverid" class="form-control">
                                    <input type="hidden" name="driverId" id="driverId" value="" />
                                    <div class="input-group-btn">
                                        <button type="button" class="btn btn-white dropdown-toggle" data-toggle="dropdown" style="padding: 6px 12px;">
                                            <span class="caret"></span>
                                        </button>
                                        <ul class="dropdown-menu dropdown-menu-right" role="menu" style="z-index: 9999;"></ul>
                                    </div>
                                    <!-- /btn-group -->
                                </div>
                            </div>

                        </div>
                        <div class="form-group">
                            <div class="col-sm-10">
                                <input type="hidden" id="radio" value="0" />
                                <div class="radio i-checks">
                                    <label>
                                        <input type="radio" checked="" value="0" name="a">
                                        <i></i> 选择餐厅
                                    </label>
                                </div>
                                <div class="radio i-checks">
                                    <label>
                                        <input type="radio" value="1" name="a">
                                        <i></i> 选择某条线
                                    </label>
                                    <div id="lineDiv" style="position:absolute;top:30px;left:20px;width:120px;display:none;">
                                        @Html.DropDownList("Line", ViewBag.Line as IEnumerable<SelectListItem>, new { @class = "form-control" })
                                    </div>
                                </div>
                                <div class="radio i-checks">
                                    <label>
                                        <input type="radio" value="2" name="a">
                                        <i></i> 全部餐厅
                                    </label>
                                </div>
                                <div class="radio i-checks">
                                    <label>
                                        <input type="radio" value="3" name="a">
                                        <i></i>半月定期
                                    </label>
                                </div>
                                <div class="radio i-checks">
                                    <label>
                                        <input type="radio" value="4" name="a">
                                        <i></i>每月定期
                                    </label>
                                </div>
                            </div>

                        </div>
                        <hr />
                        <div class="form-group">
                            <div class="col-md-4">
                                <label class="control-label col-md-3">收据备注：</label>
                                <div class="input-group col-md-9">
                                    <textarea class="form-control" style="resize:none;" rows="5"></textarea>
                                </div>
                            </div>
                        </div>
                    </form>
                    <div class="ibox-tools">

                    </div>
                </div>
                <div id="data_list">
                    @*@{Html.RenderPartial("MakingReceiptPartial", Model);}*@
                </div>
                <br />

            </div>
        </div>
    </div>
</div>
<div id="modal_ajax" class="modal inmodal fade" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
</div>
@section scripts{
    <script src="~/Theme/js/content.min.js?v=1.0.0"></script>
    <script src="~/Theme/js/plugins/iCheck/icheck.min.js"></script>
    <script src="~/Theme/js/plugins/datapicker/bootstrap-datepicker.js"></script>
    <script src="~/Theme/js/plugins/suggest/bootstrap-suggest2014.js"></script>
    <script src="~/js/DateComm.js"></script>
    <script>

        $('#beginTime').datepicker({
            todayBtn: "linked",
            autoclose: true,
            todayHighlight: true

        }).on('changeDate', function (e) {
            var startTime = e.date;
            $('#endTime').datepicker('setStartDate', startTime);
        });
        //结束时间：
        $('#endTime').datepicker({
            todayBtn: "linked",
            autoclose: true,
            todayHighlight: true

        }).on('changeDate', function (e) {
            var endTime = e.date;
            $('#beginTime').datepicker('setEndDate', endTime);
        });


        new DateContUtil({//【下拉日期】
            selectId: "data_select",//下拉框のID
            startDate: 'beginTime',//开始日期(不填默认search_startDateBase)
            endDate: 'endTime',//结束日期(不填默认search_endDateBase)
            defaultValue: '4',//本月
            selectedFun: function () {//选中后触发の方法
                $(".form-horizontal").submit();
            }
        });

        $(document).ready(function () {
            $("#form0").submit();
            $(".i-checks").iCheck({ checkboxClass: "icheckbox_square-green", radioClass: "iradio_square-green", });

            $('#chkDiv').on('ifChecked', function (event) { //ifCreated 事件应该在插件初始化之前绑定

                $("#t tbody").find('.i-checks').iCheck('check');
            });
            $('#chkDiv').on('ifUnchecked', function (event) { //ifCreated 事件应该在插件初始化之前绑定

                $("#t tbody").find('.i-checks').iCheck('uncheck');
            });

            $(".radio").on('ifChecked', function (event) {
                var v = $(this).find(":radio").val();
                $("#radio").val(v);
                if (v == "0") {
                    $("#data_list").show();
                    $("#lineDiv").hide();
                } else if (v == "1") {
                    $("#data_list").hide();
                    $("#lineDiv").show();

                } else {
                    $("#data_list").hide();
                    $("#lineDiv").hide();
                }
            });

            //绑定司机
            $.getJSON("/Users/GetSuggestData?type=司机类型").done(function (data) {
                CommonAjaxCallback(data, function () {
                    drivers = data.result;
                    bindSuggest(drivers);
                }, null);
            }).fail(function () {
                layer.msg('连接超时，获取司机信息失败！', {
                    icon: 2,
                    time: 2000
                });
            });


            $('#modal_ajax').on('show.bs.modal', function (e) {
                $(this).addClass('loading');
                var $button = $(e.relatedTarget);
                var url = $button.data('url');
                if (!!url) {
                    url = url + ((url.indexOf("?") > -1) ? "&r=" : "?r=") + Math.random();
                    $(this).load(url, function () {
                        $.validator.unobtrusive.parse(this); //$("#form_model_create").validate();
                        $("input[name='CarOld']").datepicker({ todayBtn: "linked", keyboardNavigation: !1, forceParse: !1, calendarWeeks: !0, autoclose: !0 });
                        $(".i-checks").iCheck({ checkboxClass: "icheckbox_square-green", radioClass: "iradio_square-green", });
                        $(this).removeClass('loading');


                        $('.btnDel').on('click', function () {
                            $(this).parent().parent().remove();
                        });

                    });
                }
            });
            $('#modal_ajax').on('hidden.bs.modal', function (e) {
                $(this).empty();
            });

            $('#btn').on("click", function () {
                var t = $("#t tbody").find('input[type="checkbox"]');
                var begin = $("#beginTime").val();
                var end = $("#endTime").val();
                if (begin == "" && end == "") {
                    layer.msg('请先选择时间！！', {
                        icon: 2,
                        time: 2000
                    });
                    return false;
                }
                var option = $("#radio").val();
                var url = '/Receivable/MakingReceiptDetails?beginTime=' + begin + '&endTime=' + end;
                if (option == "0") {
                    var ids = [];
                    for (var i = 0; i < t.length; i++) {
                        if ($(t[i]).prop('checked')) {
                            ids.push($(t[i]).val());
                        }
                    }
                    if (ids.length == 0) {
                        layer.msg('请至少选择一条数据！！', {
                            icon: 2,
                            time: 2000
                        });
                        return false;
                    }
                    if ($("#che").prop("checked")) {
                        var drid=0;	
                        var type=0;
                    var line = $("#Line2").val();			
                    if($("#driverId").val()!=""){
                        drid=$("#driverId").val();
                               }else{
                                  type=1;
                                }
                        url = url + '&type='+type+'&driverLine='+line+'&driverId=' +drid;
                    } else { 
                        url = url + '&ids=' + ids.join('|');

                    }
                   
                } else if (option == "1") {
                    var line = $("#Line").val();
                    url = url + '&type=1&driverLine=' + line;
                } else {
                    url = url + '&type=' + option;
                }

                top.window.openTab(1130, '预收单', url);
            })
        });
        function bindSuggest(data) {
            if ($("#dropdown-driverid") != undefined) {
                $("#dropdown-driverid").bsSuggest({
                    effectiveFields: ["name"],
                    effectiveFieldsAlias: { "name": "姓名" },
                    searchFields: ["name"],
                    clearable: true,
                    getDataMethod: "data",
                    //url: "/Users/GetSuggestData?type=司机类型&keyword=",
                    data: { value: data },
                    idField: "id",
                    keyField: "name",
                    callfun: function () {
                        $("#driverId").val("");
                    }
                }).on('onDataRequestSuccess', function (e, result) {
                    console.log('onDataRequestSuccess: ', result);
                }).on('onSetSelectValue', function (e, keyword, data) {
                    $("#driverId").val(data.id);
                    console.log('onSetSelectValue: ', keyword, data);
                }).on('onUnsetSelectValue', function () {
                    console.log('onUnsetSelectValue');
                });
            }
        }

        function ReloadAjaxCallbackgoback(data, successInfo) {
            CommonAjaxCallback(data, function () {
                $('#modal_ajax').modal("hide");
                window.location.reload();
            }, successInfo);
        }
        function LoadDataSuccessMessage() {
            layer.closeAll();
            $(".i-checks").iCheck({ checkboxClass: "icheckbox_square-green", radioClass: "iradio_square-green", });
            $('#chkDiv').on('ifChecked', function (event) { //ifCreated 事件应该在插件初始化之前绑定
                $("#t tbody").find('.i-checks').iCheck('check');
            });
            $('#chkDiv').on(' ifUnchecked', function (event) { //ifCreated 事件应该在插件初始化之前绑定
                $("#t tbody").find('.i-checks').iCheck('uncheck');
            });

        }

        $("#data_list").on("change", "#pageSize", function () {
            var size = $("option:selected", this).val();
            var pageurl = $("#pageurl").val();
            $("#data_list").load("/Receivable/MakingReceipt?size=" + size + "&r=" + Math.random() + pageurl, function () {
                $("#size").val($("option:selected", this).val());
                LoadDataSuccessMessage();
            });
        });
    </script>
}
